package com.momoc.multi.chat.room.common.utils;

import java.util.List;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Component;

/**
 * @author momoc
 * @version 1.0
 * @className RedisUtils
 * @description
 * @date 2023/8/11 14:18
 */
@RequiredArgsConstructor
@Component
public class RedisUtils {

    final RedisTemplate<String,Object> redisTemplate;
    final RedisTemplate<String, String> stringRedisTemplate;


    /**
     * 获取数据通过key
     * @param key
     * @return
     * @param <T>
     * @throws Exception
     */
    public <T>  T getObjByKey(String key) throws Exception {
        Object value = redisTemplate.opsForValue().get(key);
        return (T) value;
    }

    public void setObject(String key, Object obj, int seconds) throws Exception {
        redisTemplate.opsForValue().set(key,obj, seconds, TimeUnit.SECONDS);
    }
    public void setObject(String key, Object obj) throws Exception {
        redisTemplate.opsForValue().set(key,obj);
    }

    public String getByKey(String key) throws Exception {
        return stringRedisTemplate.opsForValue().get(key);
    }

    public void set(String key, String obj, int seconds) throws Exception {
        stringRedisTemplate.opsForValue().set(key,obj, seconds, TimeUnit.SECONDS);
    }
    public void set(String key, String obj) throws Exception {
        stringRedisTemplate.opsForValue().set(key,obj);
    }
    public Long increment(String key){
        return stringRedisTemplate.opsForValue().increment(key);
    }

    public void hashSet(String key, String field, String value) {
        stringRedisTemplate.opsForHash().put(key,field,value);
    }

    public String hashGet(String key, String field) {
        Object value = stringRedisTemplate.opsForHash().get(key, field);
        if (value == null){
            return null;
        }
        return value.toString();
    }

    public void removeKey(String key) {
        stringRedisTemplate.delete(key);
    }
    public void removeKeys(List<String> keys) {
        stringRedisTemplate.delete(keys);
    }


    public void addList(String key, String value) {
        stringRedisTemplate.opsForList().rightPush(key, value);
    }
    public String getListFirst(String key) {
        return stringRedisTemplate.opsForList().leftPop(key);
    }

    public void addSet(String key, String field) {
         stringRedisTemplate.opsForSet().add(key, field);
    }
    public List<String> popSet(String key) {
        return stringRedisTemplate.opsForSet().pop(key, 100);
    }

    public Long removeSet(String key, String field) {
        return stringRedisTemplate.opsForSet().remove(key, field);


    }


    /**
     * 批量判断当前keys是否存在
     * @param keys
     * @return
     */
    public List<Long> batchKeyExists(List<String> keys) {
        DefaultRedisScript<List> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptText("local result={}\n" +
                "for i=1, #KEYS do\n" +
                "   result[i]=redis.call('exists', KEYS[i])\n" +
                "end\n" +
                "return result");
        redisScript.setResultType(List.class);
        return stringRedisTemplate.execute(redisScript, keys);
    }


}
